//登录页面验证
(function($) {
	var username_val = null;
	var pw_val = null;
	var bStop1 = false;
	var bStop2 = false;
	var bStop3 = false;

	$('#username').on('focus', function() {
		$(this).prop('placeholder', '');
	});
	$('#username').on('blur', function() {
		if($(this).prop('placeholder') == '') {
			$(this).prop('placeholder', '用户名');
		}
		username_val = $(this).val();
		if(username_val == '') {
			$('#username_tips').html('用户名不能为空');
			bStop1 = false;
		} else {
			$('#username_tips').html('');
			bStop1 = true;
		}
	});
	$('#pw').on('focus', function() {
		$(this).prop('placeholder', '');
	});
	$('#pw').on('blur', function() {
		if($(this).prop('placeholder') == '') {
			$(this).prop('placeholder', '密码');
		}
		pw_val = $(this).val();
		if(pw_val == '') {
			$('#pw_tips').html('密码不能为空');
			bStop2 = false;
		} else {
			$('#pw_tips').html('');
			bStop2 = true;
		}
	});
	$('.register #ck').on('click', function() {
		if($(this).prop('checked') == true) {
			bStop3 = true;
		} else {
			bStop3 = false;
		}
	});
	$('#submit').on('click', function() {
		if(bStop1 == false || bStop2 == false || bStop3 == false) {
			return false;
		}else{
			$.ajax({
				type:"post",
				url:"../php/login.php",
				async:true,
				data:{
					'username':username_val,
					'password':pw_val
				}
			})
			.done(function(data){
				if (data==true) {
					addCookie('Username',username_val,7);
					window.location.href='../html/index.html';
				} else{
					$('#username_tips').html('用户名或密码错误');
					$('#pw_tips').html('');
					setTimeout(function(){
						location.reload();
					},3000);
				}
			})
			.fail(function(xhr,err){
				console.log(xhr+','+err);
			});
		}
	});
})(jQuery);